<template>
  <div class="main-commidy">
    <EditTable
      ref="editTable"
      @onChangeSelectAll="onSelectAll"
      @onJoinSale="onJoinSale"
      @onJoinSaleCheck="onJoinSaleCheck"
      :listData="columns1"
      @onDeleteSale="onDeleteSale"
      :list="$store.state.fullReturnSale.GetsaleData"
      :isLarge="true">
    </EditTable>
    <div class="pager">
      <Page
        :total="totalItem"
        :current="currentPage"
        :page-size="pageSize"
        @on-change="onPageChange"
        @on-page-size-change="onPageSizeChange"
        show-sizer
        placement="top"
      ></Page>
    </div>
    <Loader :status="status" @refresh="getSkuPage"></Loader>
  </div>
</template>
<script>
import EditTable from "@/views/sales/common/TableHasEven";
import { columns1, data1 } from "./MainCommidyMethods";
import {setArrayValue, getValueArray, setEmptyArray, getSelectStatus, getValueStatus} from "./ChoiceCommidyMethods";
import { deleteSale, onDeleteSale, deleteValueArray} from "@/views/sales/common/TableSaleMethods";
export default {
  name: "maincommidy",
  props: {
    // totalItem: {
    //   type: Number,
    //   default: () => 1
    // }
  },
  components: {
    EditTable
  },
  mounted() {
    // this.getSkuPage({size: window.$pageSize, current: 1, self: this})
  },
  data() {
    return {
      columns1,
      data1,
      id: this.$route.query.id || '',
      currentPage: 1,
      pageSize: window.$pageSize,
      totalItem: 1,
      status: 'pass'
    };
  },
  methods: {
    setPage() {
      this.currentPage = 1
      this.pageSize = window.$pageSize
      this.getSkuPage({})
    },
    getSkuPage(param) {
      param.self = this
      this.status = 'loading'
      param.activityId = this.id
      this.$store.dispatch('getOtherSkuPage', param).then(res => {
        this.$refs.editTable.selectAll = false
      })
    },
    onPageChange(val) {
      this.currentPage = val
      this.getSkuPage({size: this.pageSize, current: this.currentPage, self: this})
    },
    onPageSizeChange(val) {
      this.pageSize= val
      this.getSkuPage({size: this.pageSize, current: this.currentPage, self: this})
    },
    deleteSaleData() {
      if (!this.$store.state.fullReturnSale.GetsaleData.length) {
        this.$Notice.warning({
          title: '温馨提示',
          desc: "请选择删除的商品"
        })
        return
      }
      let ids = []
      for (let item of this.$store.state.fullReturnSale.GetsaleData) {
        for (let todo of item["productSkuList"]) {
          if (todo.checked) {
            ids.push(todo.acId)
          }
        }
      }
      this.$store.dispatch('deleteSku', {id: ids, activityId: this.id}).then(res => {
        this.status = "pass"
        this.$store.dispatch("getOtherSkuCount", {id: this.id})
        this.$Notice.success({
          title: '温馨提示',
          desc: "删除商品成功"
        })
        deleteValueArray(this.$store.state.fullReturnSale.GetsaleData, { index: "checked", childName: "productSkuList", status: true}, this.$store.state.fullReturnSale.saleData);
        setEmptyArray(this.$store.state.fullReturnSale.GetsaleData, { index: "checked", childName: "productSkuList"});
        getValueStatus(this.$store.state.fullReturnSale.GetsaleData, {index: "checked", childName: "productSkuList"});
      })
      // this.$refs.editTable.selectAll = getSelectStatus(this.$store.state.fullReturnSale.GetsaleData, {index: 'checked', childName: 'productSkuList'})
    },
    onSelectAll(param) {
      //点击选中所有checked按钮的时候出发的事件
      param ? setArrayValue(this.$store.state.fullReturnSale.GetsaleData, {
            index: "checked",
            val1: true,
            childName: "productSkuList"
          }) : setArrayValue(this.$store.state.fullReturnSale.GetsaleData, {
            index: "checked",
            val1: false,
            childName: "productSkuList"
          });
    },
    onJoinSaleBody(item, todo) {
      if (item.productSkuList && item.productSkuList.length > 0) {
        item.checked ? setArrayValue(item.productSkuList, {
              index: "checked",
              val1: true,
              childName: "productSkuList"
            }) : setArrayValue(item.productSkuList, {
              index: "checked",
              val1: false,
              childName: "productSkuList"
            });
      } else if (!item.productSkuList) {
        /* 当是子集的时候，选择一个对应的父级就选上 */
        let status = false;
        if (!todo) {
          return false;
        }
        for (let child of todo.productSkuList) {
          if (child.checked) {
            status = true;
          }
        }
        if (!status) {
          //子集没有被选中
          todo.checked = false;
        } else {
          todo.checked = true;
        }
      }
    },
    onJoinSale(item, todo) {
      //点击加入促销的时候触发的事件
      item.checked = !item.checked;
      this.onJoinSaleBody(item, todo);
    },
    onJoinSaleCheck(item, todo) {
      // 点击checked按钮的时候触发的事件
      this.onJoinSaleBody(item, todo);
    },
    onDeleteSale(index, data, index1, data1) {
      let ids = []
      if (index1) {
        ids = [data[index].acId]
      } else {
        ids = data[index].productSkuList.map(item=> {
          return item.acId
        })
      }
      this.status = "loading"
      this.$store.dispatch('deleteSku', {id: ids, activityId: this.id}).then(res => {
        this.status = "pass"
        this.$store.dispatch("getOtherSkuCount", {id: this.id})
        this.$Notice.success({
          title: '温馨提示',
          desc: "删除商品成功"
        })
        onDeleteSale (index, data, index1, data1, this.$store.state.fullReturnSale.saleData);
        // deleteSale(index, data, index1, data1);
      })
    }
  },
  watch: {
    totalItem(val) {
      this.totalItem = val
    }
  }
};
</script>